﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using JYH.Data;
using Newtonsoft.Json;
using JYH.Model;
using JYH.DataAccess;
using JYH.Framework;

namespace JYH.Business
{
    /// <summary>
    ///  SysMenuInfo
    /// </summary>
    public class SysMenuInfoBLL : BusinessBase<SysMenuInfo>
    {
        /// <summary>
        ///  SysMenuInfo
        /// </summary>
        public SysMenuInfoBLL()
        {
            myDataBase = DB.D();
            this.SqlSelect = @";Select  SysMenuInfo.*,
                                ISNULL(UpMenuInfo.MenuNO, SysMenuInfo.MenuNO) UpMenuNO,
                                ISNULL(UpMenuInfo.MenuName, SysMenuInfo.MenuName) UpMenuName
                                From SysMenuInfo
                                Left JOIn SysMenuInfo UpMenuInfo ON SysMenuInfo.UpMenuID=UpMenuInfo.MenuID
                                ";
        }

        /// <summary>
        /// 获取用户菜单
        /// </summary>
        /// <param name="UserNO"></param>
        /// <param name="Msg"></param>
        /// <returns></returns>
        public List<SysMenuInfo> GetMenu(string UserNO, string MenuType, out string Msg)
        {
            Msg = string.Empty;
            Database db = DB.D();
            List<SysMenuInfo> Menus = new List<SysMenuInfo>();
            try
            {
                Sql sql = new Sql();
                if (string.Equals(UserNO.ToUpper(), "ADMIN"))
                {
                    sql.Append(@";Select SysMenuInfo.* From SysMenuInfo");
                    sql.Where("SysMenuInfo.MenuType='" + MenuType + "'");
                    sql.OrderBy("SysMenuInfo.MenuNO");
                }
                else
                {
                    sql.Append(@";Select Distinct SysMenuInfo.* from SysMenuInfo
                                    Inner JOin SysRoleMenu ON SysRoleMenu.MenuID=sysmenuinfo.MenuID
                                    Inner JOin SysRoleUser ON SysRoleMenu.RoleID=SysRoleUser.RoleID
                                    Inner JOin SysUserInfo ON SysRoleUser.UserID=SysUserInfo.UserID
                                    ");
                    sql.Where("SysUserInfo.UserNO='" + UserNO + "'");
                    sql.Where("SysMenuInfo.MenuType='" + MenuType + "'");
                    sql.OrderBy("SysMenuInfo.MenuNO");
                }
                Menus = db.Fetch<SysMenuInfo>(sql.SQL);

                return Menus;
            }
            catch (Exception ex)
            {
                Msg = ex.Message;
                return null;
            }
        }

        /// <summary>
        /// 获取所有的菜单信息
        /// </summary>
        /// <returns></returns>
        public List<SysMenuInfo> GetMenu()
        {
            List<SysMenuInfo> list = new List<SysMenuInfo>();
            if (myDataBase == null)
            {
                myDataBase = DB.D();
            }
            Sql sql = new Sql();
            sql.Append(this.SqlSelect);
            sql.OrderBy(@"SysMenuInfo.MenuType,ISNULL( SysMenuInfo.NameSpace,''),
                          ISNULL( UpMenuInfo.MenuNO, UpMenuInfo.MenuNO),SysMenuInfo.MenuNO");
            list = myDataBase.Fetch<SysMenuInfo>(sql);
            return list;
        }

        public bool SaveMenuInfo(SysMenuInfo Info, out string Msg)
        {
            Msg = string.Empty;
            try
            {
                if (myDataBase == null)
                {
                    myDataBase = DB.D();
                }
                if (string.IsNullOrEmpty(Info.MenuID))
                {
                    Info.MenuID = Guid.NewGuid().ToString();
                }
                SysMenuInfo obj = new SysMenuInfo();
                Sql sql = new Sql();
                sql.Where("(SysMenuInfo.MenuID='" + Info.MenuID + "' )");
                obj = this.GetSingeModel(sql);
                if (obj == null)
                {
                    obj = new SysMenuInfo();
                    obj.MenuID = Guid.NewGuid().ToString();
                    obj.MenuNO = Info.MenuNO;
                    obj.MenuName = Info.MenuName;
                    obj.MenuUrl = Info.MenuUrl;
                    obj.MenuType = Info.MenuType;
                    obj.NameSpace = Info.NameSpace;
                    obj.ClassName = Info.ClassName;
                    obj.UpMenuID = Info.UpMenuID;
                    obj.RState = "2";
                    obj.CreateTime = BusinessTool.GetDateTime();
                    obj.CreateTime = CookieHelper.ReadCookie();
                    myDataBase.Execute(BusinessTool.GetInsertSql(obj));
                }
                else
                {
                    obj.MenuNO = Info.MenuNO;
                    obj.MenuName = Info.MenuName;
                    obj.MenuUrl = Info.MenuUrl;
                    obj.MenuType = Info.MenuType;
                    obj.NameSpace = Info.NameSpace;
                    obj.ClassName = Info.ClassName;
                    obj.UpMenuID = Info.UpMenuID;
                    obj.CreateTime = BusinessTool.GetDateTime();
                    obj.CreateTime = CookieHelper.ReadCookie();
                    myDataBase.Execute(BusinessTool.GetUpdateSql(obj));
                }
                return true;
            }
            catch (Exception ex)
            {
                Msg += ex.Message;
                return false;
            }
        }

        /// <summary>
        /// 菜单删除
        /// </summary>
        /// <param name="MenuID"></param>
        /// <param name="Msg"></param>
        /// <returns></returns>
        public override bool Delete(string MenuID, out string Msg)
        {
            Msg = string.Empty;
            if (myDataBase == null)
            {
                myDataBase = DB.D();
            }
            myDataBase.BeginTransaction();
            try
            {
                Sql sql = new Sql();
                sql.Append("Select SysMenuInfo.* From SysMenuInfo");
                sql.Where(@"SysMenuInfo.UpMenuID='" + MenuID + "'");
                List<SysMenuInfo> list = myDataBase.Fetch<SysMenuInfo>(sql);
                if (list.Count > 0)
                {
                    Msg = "The submenu cannot be deleted. ";
                    return false;
                }
                StringBuilder sb = new StringBuilder();
                sb.Append(@"Delete SysRoleMenu Where MenuID='" + MenuID + "'");
                sb.Append(@"Delete SysMenuButton Where MenuID='" + MenuID + "'");
                sb.Append(@"Delete SysMenuInfo Where MenuID='" + MenuID + "'");
                myDataBase.Execute(sb.ToString());
                myDataBase.CompleteTransaction();

                return true;
            }
            catch (Exception ex)
            {
                Msg = ex.ToString();
                myDataBase.AbortTransaction();
                return false;
            }
        }
    }

    /// <summary>
    /// PDA菜单模组类
    /// </summary>
    public class MenuModule
    {
        /// <summary>
        /// 模组名称
        /// </summary>
        public string ModuleName { get; set; }

        /// <summary>
        /// 菜单名称
        /// </summary>
        public List<SysMenuInfo> Menus { get; set; } = new List<SysMenuInfo>();
    }
}